package com.zh.common.plugins.shiro;

import com.zh.common.utils.ResultEnum;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Map;

/**
 *
 * @ProjectName:
 * @Package:        com.zh.common.plugins.shiro
 * @ClassName:      ShiroExceptionHandler
 * @Description:    hiro异常处理类
 * @Author:         lds
 * @CreateDate:     2019/5/22 0022 20:01
 * @UpdateUser:     更新者
 * @UpdateDate:     2019/5/22 0022 20:01
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
@ControllerAdvice
public class ShiroExceptionHandler {

    /**
     * 未认证异常处理
     * @return
     */
    @ResponseBody
    @ExceptionHandler(UnauthenticatedException.class)
    public Map<String, Object> authenticationException() {
        return ResultEnum.UNAUTHENTIC.result();
    }

    /**
     * 未授权异常处理
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = UnauthorizedException.class)
    public Map<String, Object> authorizationException() {
        return ResultEnum.UNAUTHORIZED.result();
    }

    /**
     * 捕捉404异常
     * @return
     */
    @ResponseBody
    @ExceptionHandler(NoHandlerFoundException.class)
    public Map<String, Object> handle404(NoHandlerFoundException e) {
        return ResultEnum.NOTFOUND.result();
    }


}
